
function promiseAll(promises) {
    return new Promise((resolve,reject) => {
        if(!Array.isArray(promises)) {
            throw new Error('must array!')
        }
        var resolvedCounter = 0;
        var promiseNum = promises.length;
        var resolveResult = [];
        for(let i=0;i<promiseNum;i++) {
            // 启动并发
            Promise.resolve(promises[i])
                .then(value => {
                    resolvedCounter++;
                    resolveResult[i] = value;
                    if(resolvedCounter == promiseNum){
                        return resolve(resolveResult);
                    }
                },err => {
                    return reject(err)
            })
        }
    })
}
// 测试用例 驱动写并正确写代码
let p1 = new Promise(function(resolve,reject) {
    setTimeout(function() {
        resolve(1)
    },2000)
})
let p2 = new Promise(function(resolve,reject) {
    setTimeout(function() {
        resolve(2)
    },2000)
})
let p3 = new Promise(function(resolve,reject) {
    setTimeout(function() {
        resolve(3)
    },2000)
})

promiseAll([p1,p2,p3])
    .then(res => {
        console.log(res);
    })
    .catch(err => {
        console.log(err);
    })